package com.example.it.looker.presenter;

import com.example.it.looker.model.datasource.NewsDataSource;
import com.example.it.looker.model.entity.News;
import com.example.it.looker.presenter.contact.NewsContact;

import java.util.List;

/**
 * Created by 鲁迅认识的那只猹 on 8/31/2017 10:41 PM.
 * Emial 1258730808@qq.com
 * Desc:
 */

public class NewsPresenter implements NewsContact.NewsPresenter {

    private NewsDataSource dataSource;
    private NewsContact.NewsView newsView;

    public NewsPresenter(NewsContact.NewsView newsView, NewsDataSource dataSource) {
        this.newsView = newsView;
        this.dataSource = dataSource;
    }

    @Override
    public void loadData() {
        newsView.loading();
        new Thread(new Runnable() {
            @Override
            public void run() {
                List<News> newsList = dataSource.getLastNews(new NewsDataSource.NewsListLoadedCallBack() {
                    @Override
                    public void newsListLoadedCallBack(List<News> newsList, List<News> topNews) {
                        newsView.topNewsLoaded(topNews);
                        newsView.loaded(newsList);
                    }
                });
            }
        }).start();
    }

    @Override
    public void loadMore() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                List<News> beforeNewsList = dataSource.LoadMore();
                newsView.loadedMore(beforeNewsList);
            }
        }).start();

    }

    @Override
    public void onDestroy() {
        newsView = null;
    }
}
